package cn.graydove.bot.listener;

import cn.graydove.bot.core.Assert;
import cn.graydove.bot.exception.FileErrorException;
import cn.graydove.bot.model.entity.Advance;
import cn.graydove.bot.model.entity.Image;
import cn.graydove.bot.service.AdvanceService;
import cn.graydove.bot.service.ImageService;
import cn.graydove.bot.support.BotProperties;
import cn.graydove.bot.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@Slf4j
public class FileListener {

    private Assert asserts;

    private ImageService imageService;

    private AdvanceService advanceService;

    private BotProperties botProperties;

    public FileListener(Assert asserts, ImageService imageService, AdvanceService advanceService, BotProperties botProperties) {
        this.asserts = asserts;
        this.imageService = imageService;
        this.advanceService = advanceService;
        this.botProperties = botProperties;
    }

//    @Listener(EventType.GROUP_MESSAGE)
//    @Filter({"^涩图[\\s]*[\\d]*[\\s]*$", "^来一份涩图$"})
//    public void seImg(GroupMessageEvent event, RobotClient client) {
//        asserts.groupReceive(event, group -> {
//            Advance advance = advanceService.findOrCreateByGroupNumber(event.getGroupId());
//            if (advance.getStatus() == null || advance.getStatus() < 2) {
//                if (advanceService.isActivated(advance) || advance.getRequestImageNumber() < botProperties.getImageUpperLimit()) {
//                    Pattern compile = Pattern.compile("^涩图[\\s]*([\\d]*)[\\s]*$");
//                    Matcher matcher = compile.matcher(event.getMessage());
//                    Image image;
//                    if (matcher.find()) {
//                        try {
//                            int id = Integer.parseInt(matcher.group(1).trim());
//                            image = imageService.findById(id);
//                            if (image == null) {
//                                client.sendGroupMsg(event.getGroupId(), "该图片不存在");
//                                return;
//                            }
//                        } catch (Exception e) {
//                            image = imageService.getRandomOne();
//                        }
//                    } else {
//                        image = imageService.getRandomOne();
//                    }
//                    if (image != null) {
//                        String sb = CommonUtils.file(imageService.getVisitUrl(image.getFilename())) +
//                                "\n图片ID：" +
//                                image.getId() +
//                                "\n上传时间：" +
//                                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(image.getCreateTime());
//                        advanceService.addImageNumber(event.getGroupId());
//                        client.sendGroupMsg(event.getGroupId(), sb);
//                    } else {
//                        client.sendGroupMsg(event.getGroupId(), "暂无涩图");
//                    }
//                } else {
//                    client.sendGroupMsg(event.getGroupId(), "非内测群该功能每天只可使用" + botProperties.getImageUpperLimit() + "次，每上传一张图片当天使用次数+1\n今天已达到上限，请明天再试或者成功上传一张图片后再尝试");
//                }
//            } else {
//                client.sendGroupMsg(event.getGroupId(), "此功能已被封禁，如需申诉请联系作者");
//            }
//        });
//    }
//
//    @Listener(EventType.GROUP_MESSAGE)
//    @Filter("^上传涩图.*")
//    public void uploadSeImg(GroupMessageEvent event, RobotClient client) {
//        asserts.groupReceive(event, group -> {
//            Advance advance = advanceService.findOrCreateByGroupNumber(event.getGroupId());
//            if (advance.getStatus() != null && advance.getStatus() < 1) {
//                String message = event.getMessage();
//                Pattern pattern = Pattern.compile("file=(.*),url=(.*)[,\\]]");
//                Matcher matcher = pattern.matcher(message);
//                if (matcher.find()) {
//                    try {
//                        String file = matcher.group(1);
//                        String url = matcher.group(2);
//
//                        Image image = imageService.upload(CommonUtils.getName(event), event.getUserId(), event.getGroupId(), url, file);
//                        advanceService.ebbImageNumber(event.getGroupId());
//                        log.info("上传涩图{}:{}", image.getId(), image.getFilename());
//                        client.sendGroupMsg(event.getGroupId(), "图片已保存，ID:" + image.getId());
//                    } catch (FileErrorException e) {
//                        client.sendGroupMsg(event.getGroupId(), e.getMessage());
//                    } catch (Exception e) {
//                        log.error("保存图片发生错误:{}", e.getMessage());
//                        client.sendGroupMsg(event.getGroupId(), "保存图片发生错误");
//                    }
//                } else {
//                    client.sendGroupMsg(event.getGroupId(), "请同时发送图片");
//                }
//            } else {
//                client.sendGroupMsg(event.getGroupId(), "因成本太高作者已无力承担，此功能已被封禁。若涩图功能流量用尽，也将停用");
//            }
//
//        });
//
//    }
//
//    @Listener(EventType.GROUP_MESSAGE)
//    @Filter("^删除涩图 [\\d]+")
//    public void delSeImg(GroupMessageEvent event, RobotClient client) {
//        if (event.getUserId() == botProperties.getHostQq()) {
//            try {
//                String[] s = event.getMessage().split(" ");
//                int id = Integer.parseInt(s[1]);
//
//                Image img = imageService.findById(id);
//
//                if (img != null) {
//                    imageService.deleteById(id);
//                    client.sendGroupMsg(event.getGroupId(), "删除成功");
//                } else {
//                    client.sendGroupMsg(event.getGroupId(), "图片不存在");
//                }
//
//            } catch (Exception e) {
//                client.sendGroupMsg(event.getGroupId(), "请输入正确id");
//            }
//        }
//    }
//
//    @Listener(EventType.GROUP_MESSAGE)
//    @Filter("^涩图\\*[\\d]+")
//    public void multiImg(GroupMessageEvent event, RobotClient client) {
//        log.info(event.getMessage());
//        if (event.getUserId() == botProperties.getHostQq()) {
//            try {
//                String[] s = event.getMessage().split("\\*");
//                int num = Integer.parseInt(s[1]);
//
//                if (num > 10) {
//                    num = 10;
//                }
//                StringBuilder sb = new StringBuilder(String.valueOf(num)).append("张涩图：\n");
//                for (int i=0;i<num;++i) {
//                    Image one = imageService.getRandomOne();
//                    sb.append(CommonUtils.file(imageService.getVisitUrl(one.getFilename()))).append("\n");
//                }
//                client.sendGroupMsg(event.getGroupId(), sb.toString());
//
//            } catch (Exception e) {
//                client.sendGroupMsg(event.getGroupId(), "请输入正确的数量");
//            }
//        }
//    }

}
